﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Printing;

namespace 提交管理平台V1
{
    public partial class PrinterSettingsFrm : Form
    {
        DataTable dtPrinterSettings;
 
        public PrinterSettingsFrm()
        {
            InitializeComponent();
        }

        private void PrinterSettingsFrm_Load(object sender, EventArgs e)
        {
            iniPrinterSettings();
            dataGridView1.DataSource = dtPrinterSettings;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            iniTempletComboBox();
            iniPrintersComboBox();
            dataGridView1.AutoResizeColumns();
        }

        private void iniPrinterSettings()
        {
            if (dtPrinterSettings == null)
            {
                dtPrinterSettings = new DataTable();
                dtPrinterSettings.Columns.Add("模板");
                dtPrinterSettings.Columns.Add("指定打印机");
            }
            else
            {
                dtPrinterSettings.Rows.Clear();
            }
            if (Properties.Settings.Default.PrinterSettings.Length > 0)
            {
                string[] tmpSettings = Properties.Settings.Default.PrinterSettings.Split(';');
                if (tmpSettings.Length % 2 != 0)
                {
                    MessageBox.Show("打印机设置有误，请检查后重新设置！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    this.Close();
                }
                DataRow r;
                for (int i=0;i<tmpSettings .Length -1;i+=2)
                {
                    r = dtPrinterSettings.NewRow();
                    r["模板"] = tmpSettings[i];
                    r["指定打印机"] = tmpSettings[i + 1];
                    dtPrinterSettings.Rows.Add(r);
                }
            }
        }

        private void iniTempletComboBox()
        {
            DirectoryInfo di = new DirectoryInfo(Application.StartupPath+"\\template\\");
            FileInfo[] fi = di.GetFiles("*.rpt",SearchOption.AllDirectories );
            if (fi.Length ==0)
            {
                this.Close ();
            }
            for (int i = 0; i < fi.Length; i++)
            {
                comboBox1.Items.Add(fi[i].Name.Substring (0,fi [i].Name .Length -4));
            }
        }

        private void iniPrintersComboBox()
        {
            try
            {
                foreach (string Printer in PrinterSettings.InstalledPrinters)
                {
                    comboBox2.Items.Add(Printer);
                }
            }
            catch
            {
                MessageBox.Show("获取打印机名错误！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.Close();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (comboBox1.Text.Length > 0 && comboBox2.Text.Length > 0)
            {
                int ind=GetSettingIndex(comboBox1.Text);
                if ( ind == -1)
                {
                    DataRow r = dtPrinterSettings.NewRow();
                    r["模板"] = comboBox1.Text;
                    r["指定打印机"] = comboBox2.Text;
                    dtPrinterSettings.Rows.Add(r);
                    SaveSettings();
                    iniPrinterSettings();
                    dataGridView1.AutoResizeColumns();
                }
                else
                {
                    dtPrinterSettings.Rows[ind][1] = comboBox2.Text;
                    SaveSettings();
                    iniPrinterSettings();
                    dataGridView1.AutoResizeColumns();
                }
            }
        }

        private void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow != null)
            {
                dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);
                SaveSettings();
                iniPrinterSettings();
                dataGridView1.AutoResizeColumns();
            }
        }

        private int GetSettingIndex(string strTempletName)
        {
            int ret = -1;
            for (int i = 0; i < dtPrinterSettings.Rows.Count; i++)
            {
                if (dtPrinterSettings.Rows[i]["模板"].ToString () == strTempletName)
                {
                    ret = i;
                    break;
                }
            }
            return ret;
        }

        private void SaveSettings()
        {
            string tmpPrinterSettings = "";
            if (dtPrinterSettings.Rows.Count > 0)
            {
                for (int i = 0; i < dtPrinterSettings.Rows.Count; i++)
                {
                    tmpPrinterSettings += dtPrinterSettings.Rows[i][0].ToString() + ";" + dtPrinterSettings.Rows[i][1].ToString();
                    if (i < dtPrinterSettings.Rows.Count - 1)
                    {
                        tmpPrinterSettings += ";";
                    }
                }
            }
            Properties.Settings.Default.PrinterSettings = tmpPrinterSettings;
            Properties.Settings.Default.Save();
        }
    }
}